package com.mdh.runerrands.controller;

import com.auth0.jwt.JWT;
import com.mdh.runerrands.Util.JwtUtil;
import com.mdh.runerrands.Util.PassToken;
import com.mdh.runerrands.Util.Result;
import com.mdh.runerrands.pojo.BaseUser;
import com.mdh.runerrands.pojo.UserInfo;
import com.mdh.runerrands.service.BaseUserService;
import com.mdh.runerrands.service.IUserInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/controller/user")
public class UserInfoController {
    @Resource
    private BaseUserService baseUserService;

    @Resource
    private IUserInfoService userInfoService;
    @PassToken
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Result login(String username ,String password){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            BaseUser byUsername = baseUserService.findByUsername(username);
            if (byUsername == null || !byUsername.getPassword().equals(password) ){
                result.setMessage("用户信息不符");
                return result;
            }else {
                UserInfo userInfo = new UserInfo();
                String sign = JwtUtil.sign(byUsername.getId(), password);
                Map<String,String> map = new HashMap();
                map.put("token",sign);
                result.setData(map);
                userInfo.setUserId(byUsername.getId());
                userInfo.setToken(sign);
                userInfoService.insert(userInfo);
                result.setMessage("success");
                return result;
            }
            }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("false");
            result.setStatus("500");
            return result;
            }
    }
    @ApiOperation("登出")
    @RequestMapping(value = "/loginOut",method = RequestMethod.GET)
    public Result login(HttpServletRequest request){

        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            String token = request.getHeader("token");
            Integer userId = JWT.decode(token).getClaim("userId").asInt();
            boolean delete = userInfoService.delete(userId);
            result.setData(delete);
            result.setMessage("success");
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("false");
            result.setStatus("500");
            return result;
        }
    }
    @ApiOperation("根据token获取用户信息")
    @RequestMapping(value = "/info",method = RequestMethod.POST)
    public Result info(String token){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            Integer userId = JWT.decode(token).getClaim("userId").asInt();
            Map map = new HashMap();
            BaseUser baseUserById = baseUserService.findBaseUserById(userId);
            if (baseUserById == null){
                result.setMessage("false");
                result.setStatus("500");
                return result;
            }
            map.put("id",baseUserById.getId());
            map.put("name",baseUserById.getUsername());
            String[] role = {baseUserById.getType()};
            map.put("role",role);
            result.setData(map);
            result.setMessage("success");
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("false");
            result.setStatus("500");
            return result;
        }
    }

}
